﻿using SNMP;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows.Forms;

namespace SNMP_Manager
{
    public partial class ManagerForm : Form
    {
        /// <summary>
        /// 
        /// </summary>
        Session session;

        /// <summary>
        /// 
        /// </summary>
        public ManagerForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagerForm_Load(object sender, System.EventArgs e)
        {
            SNMPManager.Startup();
            this.session = SNMPManager.CreateSession(this.Handle);
            this.session.ReceiveData += session_ReceiveData;
            this.session.Timeout += session_Timeout;
            this.session.ReceiveTrap += session_ReceiveTrap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.session.Close();
            SNMPManager.Cleanup();
        }

        /// <summary>
        /// 发送字节数组数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendByteArray_Click(object sender, System.EventArgs e)
        {
            // 湿度センサ１故障_湿度値【１】正常→注意→異常→注意→正常.pcap
            //System.IO.FileStream fs = new System.IO.FileStream(@"F:\data.txt", System.IO.FileMode.Open);
            /*
            System.IO.StreamReader sr = new System.IO.StreamReader(@"F:\data.txt");
            string data = sr.ReadToEnd();
            string[] dataArray = data.Split(',');
            byte[] originalData = new byte[dataArray.Length];
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < dataArray.Length; i++)
            {
                originalData[i] = Convert.ToByte(dataArray[i], 16);
                sb.Append(originalData[i].ToString() + ",");
            }
            */
            byte[] originalData = { 0, 11, 151, 162, 216, 23, 0, 96, 220, 0, 0, 224, 8, 0, 69, 0, 0, 186, 0, 0, 
                                      64, 0, 128, 17, 19, 18, 192, 168, 50, 220, 192, 168, 50, 244, 4, 2, 0, 162, 0, 
                                      166, 55, 190, 48, 129, 155, 2, 1, 1, 4, 6, 112, 117, 98, 108, 105, 99, 167, 129, 
                                      141, 2, 1, 21, 2, 1, 0, 2, 1, 0, 48, 129, 129, 48, 13, 6, 8, 43, 6, 1, 2, 1, 1, 3, 0, 
                                      67, 1, 0, 48, 27, 6, 10, 43, 6, 1, 6, 3, 1, 1, 4, 1, 0, 6, 13, 43, 6, 1, 4, 1, 129, 183, 
                                      125, 1, 5, 3, 133, 94, 48, 20, 6, 12, 43, 6, 1, 4, 1, 129, 183, 125, 1, 5, 5, 15, 66, 
                                      4, 1, 112, 1, 1, 48, 24, 6, 9, 43, 6, 1, 2, 1, 25, 1, 2, 0, 4, 11, 7, 220, 9, 3, 13, 50, 
                                      32, 0, 43, 9, 0, 48, 35, 6, 12, 43, 6, 1, 4, 1, 129, 183, 125, 1, 5, 5, 16, 4, 19, 136, 
                                      174, 144, 236, 95, 142, 188, 147, 120, 131, 90, 131, 147, 131, 84, 129, 91, 130, 
                                      80 };
            this.session.SendMessage(originalData);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void session_ReceiveData(Object sender, ReceiveDataEventArgs e)
        {
            int x = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void session_ReceiveTrap(object sender, ReceiveDataEventArgs e)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void session_Timeout(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = false;
            Dictionary<String, Value> retValueCollection = this.session.TraverseMIB("192.168.0.89", "public", "1.3.6.1.4.1.23549.1.5");

            while (true)
            {
                Entity srcEntity = this.session.CreateEntity("192.168.0.78", 1000, 3);
                Entity dstEntity = this.session.CreateEntity("192.168.0.89", 1000, 3);
                VariableBindingsList vbl = this.session.CreateVariableBindingsList();
                vbl.Add("1.3.6.1.4.1.23549.1.5.2.6.1.1.0", new Value(Value.Syntax.Octets, String.Empty));
                PduData pduData = this.session.CreatePdu(PduData.PduType.Get, 0, 0, 0, vbl);
                Community comm = this.session.CreateCommunity("public");
                this.session.SendMessage(srcEntity, dstEntity, comm, pduData);
                vbl.Clear();
                dstEntity.Clear();
                srcEntity.Clear();
                pduData.Clear();
                comm.Clear();
                Thread.Sleep(10);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
            this.session.Close();
            SNMP.SNMPManager.Cleanup();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Entity srcEntity = this.session.CreateEntity(this.GetLocalhostIPAddress(), 1000, 3);
            IPAddress dstIPAddress = IPAddress.Parse(this.txtIPAddress.Text);
            Entity dstEntity = this.session.CreateEntity(dstIPAddress, 1000, 3);
            VariableBindingsList vbl = this.session.CreateVariableBindingsList();
            vbl.Add("1.1", new Value(Value.Syntax.Octets, String.Empty));
            PduData pduData = this.session.CreatePdu(PduData.PduType.Get, 0, 0, 0, vbl);
            Community comm = this.session.CreateCommunity("public");
            this.session.SendMessage(srcEntity, dstEntity, comm, pduData);
            vbl.Clear();
            dstEntity.Clear();
            srcEntity.Clear();
            pduData.Clear();
            comm.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private IPAddress GetLocalhostIPAddress()
        {
            // IPGlobalProperties compProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)
                {
                    foreach (var x in adapter.GetIPProperties().UnicastAddresses)
                    {
                        if (x.Address.AddressFamily.ToString() == "InterNetwork")
                        {
                            return x.Address;
                        }
                    }
                }
            }
            return null;
        }
    }
}
